package com.ctrl.riveradministrator.mvp.view.activity.problem_list;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.Problem;
import com.ctrl.riveradministrator.mvp.model.Suggest;
import com.ctrl.riveradministrator.mvp.presenter.ProblemListPresenter;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.List;

public class ProblemDetail extends BaseActivity<ProblemListPresenter> implements ProblemListView {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;

    private CardView c5;
    private ConstraintLayout c44;
    private Button t22;
    private Button submit;
    private TextView suggestM, suggestD, suggestR,res,people;
    private Problem problem;
    private SimpleDraweeView imageView,imageView1;
    private GenericDraweeHierarchyBuilder builder;
    private GenericDraweeHierarchy hierarchy,hierarchy1;
    private ImageRequest request,request1;
    private DraweeController controller,controller1;
    private Uri uri;

    /**
     * 初始化bundle参数
     *
     * @param parms parms
     */
    @Override
    protected void initParams(Bundle parms) {
        problem = (Problem) getIntent().getSerializableExtra("problem");
    }

    /**
     * 绑定布局
     *
     * @return 布局ID
     */
    @Override
    protected int bindLayout() {
        return R.layout.activity_problem_detail;
    }

    /**
     * 初始化控件
     *
     * @param view view
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        leftMenu = (ImageView) $(R.id.left_iv);
        c5 = (CardView) $(R.id.c5);
        t22 = (Button) $(R.id.t22);
        submit = (Button) $(R.id.submit);
        suggestM = (TextView) $(R.id.suggest_manager);
        suggestD = (TextView) $(R.id.content);
        suggestR = (TextView) $(R.id.reply_manager);
        res = (TextView) $(R.id.res);
        people = (TextView) $(R.id.people);
        imageView = (SimpleDraweeView) $(R.id.b_im);
        imageView1 = (SimpleDraweeView) $(R.id.res_im);
        c44 = (ConstraintLayout) $(R.id.c44);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText("问题详情");
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    /**
     * 业务操作
     *
     * @param mContext context
     */
    @Override
    protected void doBusiness(Context mContext) {
        switch (problem.getProcessingState()) {
            case "1":
                c44.setVisibility(View.GONE);
                suggestM.setText("未分配");
                break;
            case "2":
                c44.setVisibility(View.GONE);
                suggestM.setText("未处理");
                c5.setVisibility(View.VISIBLE);
                break;
            case "3":
                c44.setVisibility(View.VISIBLE);
                suggestM.setText("已处理");
                t22.setVisibility(View.GONE);
                submit.setVisibility(View.GONE);
                res.setText(problem.getDisposeDescribe());
                people.setText(problem.getDistributivePersonName());
                Uri uri = Uri.parse(Constants.BASE_URL_ + problem.getDisposeImagesUrl());
                hierarchy1 = imageView1.getHierarchy();
                hierarchy1.setFailureImage(R.mipmap.ic_launcher);
                hierarchy1.setProgressBarImage(new ProgressBarDrawable());
                if (problem.getProblemImagesUrl() == null || problem.getProblemImagesUrl().equals("")) {
                    return;
                }
                request1 = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(100, 100))
                        .build();
                controller1 = Fresco.newDraweeControllerBuilder()
                        .setOldController(imageView1.getController())
                        .setImageRequest(request1)
                        .build();
                imageView1.setController(controller1);
                break;
            default:
                break;
        }
        suggestD.setText(problem.getProblemDescribe());
        suggestR.setText(problem.getCreatTime());
        Uri uri = Uri.parse(Constants.BASE_URL_ + problem.getProblemImagesUrl());
        hierarchy = imageView.getHierarchy();
        hierarchy.setFailureImage(R.mipmap.ic_launcher);
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        //imageView.setImageURI(uri);
        if (problem.getProblemImagesUrl() == null || problem.getProblemImagesUrl().equals("")) {
            return;
        }
        request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(100, 100))
                .build();
        controller = Fresco.newDraweeControllerBuilder()
                .setOldController(imageView.getController())
                .setImageRequest(request)
                .build();
        imageView.setController(controller);
    }

    /**
     * View点击
     *
     * @param view view
     */
    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    /**
     * 设置监听
     */
    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        t22.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProblemDetail.this, AssignmentiActivity.class).putExtra("problemId", problem.getProblemId()));
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProblemDetail.this, CopeActivity.class)
                        .putExtra("problemId", problem.getProblemId())
                        .putExtra("processingState", problem.getProcessingState()));
            }
        });
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected ProblemListPresenter createPresenter() {
        return new ProblemListPresenter(this);
    }

    @Override
    public void showMessage(List<Problem> suggests) {
        Toast.makeText(ProblemDetail.this, "反馈成功", Toast.LENGTH_SHORT).show();
    }
}